package com.HomeWork;

import java.io.*;

/**
 * 4.  请用多线程实现目录拷贝，提示：遇到一个目录就创建一个线程遍历该目录下的文件及目录，
 * 遇到文件就做文件的copy.再遇到目录就继续创建线程遍历该目录下的文件及目录。
 */
class thread1 extends Thread {
    File file1 = new File("G:\\js\\zxplunbo");
    File file2 = new File("D:\\js");

    public thread1(File file1, File file2) {
        this.file1 = file1;
        this.file2 = file2;
    }

    public thread1() {

    }


    public static void CopyDir(File sourceDir, File sourceFile, File targetDir) {

        File[] files = sourceFile.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    CopyDir(sourceDir, f, targetDir);


                } else {
                    String path = f.getAbsolutePath();
                    String sourcDirPath = sourceDir.getAbsolutePath();

                    String sourcParentDir = sourceDir.getParent();

                    path = targetDir.getAbsolutePath() + "\\" + path.substring(sourcParentDir.length());//从
                    //System.out.println("截取子串后的结果:"+path);

                    File targetFile = new File(path);
                    File parentDir = targetFile.getParentFile();
                    if (!parentDir.exists()) {
                        parentDir.mkdirs();
                    }
                    boolean flag = f.renameTo(targetFile);
                    System.out.println(f.getAbsolutePath() + "移动结果:" + flag);

                }

            }
        }

    }

    public static void copyF(File srcDir, File targetDir) throws IOException {
        if (srcDir.isDirectory() && targetDir.exists()) {
            CopyDir(srcDir, srcDir, targetDir);
        } else {
            targetDir.createNewFile();

        }

    }


    @Override
    public void run() {
        try {
            copyF(this.file1, this.file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //work4
        //super.run();
    }
}

public class work4 extends Thread {
    public static void main(String[] args) throws IOException {
        File file1 = new File("G:\\js\\lo\\html");
        File file2 = new File("D:\\KSWJJ");
        //CopyDir2(file1,file2);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    copyF(file1, file2);
                    System.out.println(Thread.currentThread());
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
//        thread1 thread = new thread1();
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    public static void CopyDir2(File sourceDir,  File targetDir2) {
        File[] files = sourceDir.listFiles();
        FileInputStream inputStream=null;
        FileOutputStream  fileOutputStream=null;
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    CopyDir2(f, targetDir2);
                } else {
                    try {
                        inputStream = new FileInputStream(sourceDir);
                        //FileOutputStream 构造方法参数传入的file对象，在构造方法内部分自动创建，如果文件的目录存在，则创建。

                        fileOutputStream = new FileOutputStream(targetDir2);
                        //读文件
                        byte[] buf = new byte[2048];
                        int len =-1;
                        while( (len = inputStream.read(buf))!=-1){
                            //System.out.println("读到的数据为:"+new String(buf,0,len));
                            //  fileOutputStream.write(buf);
                            fileOutputStream.write(buf,0,len);
                        }

                        System.out.println("文件复制完成");
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch ( IOException e){
                        e.printStackTrace();
                    }finally {
                        StreamUtils.close(inputStream,fileOutputStream);
                    }
                }
            }
        }
    }


    public static void CopyDir(File sourceDir, File sourceFile, File targetDir) {

        File[] files = sourceFile.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {

                    CopyDir(sourceDir, f, targetDir);
                    thread1 thread = new thread1();
                    thread.start();

                } else {
                    String path = f.getAbsolutePath();
                    String sourcDirPath = sourceDir.getAbsolutePath();

                    String sourcParentDir = sourceDir.getParent();

                    path = targetDir.getAbsolutePath() + "\\" + path.substring(sourcParentDir.length());//从
                    //System.out.println("截取子串后的结果:"+path);

                    File targetFile = new File(path);
                    File parentDir = targetFile.getParentFile();
                    if (!parentDir.exists()) {
                        parentDir.mkdirs();
                    }
                    boolean flag = f.renameTo(targetFile);
                    System.out.println(f.getAbsolutePath() + "移动结果:" + flag);

                }

            }
        }

    }

    public static void copyF(File srcDir, File targetDir) throws IOException {
        if (srcDir.isDirectory() && targetDir.exists()) {
            CopyDir(srcDir, srcDir, targetDir);

        } else {
            targetDir.createNewFile();
        }

    }
}

